package android_serialport_api;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android_serialport_api.utils.MyFunc;

/**
 * 串口辅助工具类
 */
public class SerialHelper {
	private SerialPort mSerialPort;
	private OutputStream mOutputStream;
	private InputStream mInputStream;
	private ReadThread mReadThread;
	private String sPort = "/dev/ttyS0";
	private int iBaudRate = 9600;
	private boolean _isOpen = false;

	private SerialDateReceiveCallback dateReceiveCallback;

	public SerialHelper(String sPort, int iBaudRate) {
		this.sPort = sPort;
		this.iBaudRate = iBaudRate;
		System.out.println("hww" + "SerialHelper-->sPort:" + sPort + " iBaudRate:" + iBaudRate);
	}

	public void setDataReceiveCallback(SerialDateReceiveCallback dateReceiveCallback) {
		this.dateReceiveCallback = dateReceiveCallback;
	}

	public void open() {
		close();
		try {

			mSerialPort = new SerialPort(new File(sPort), iBaudRate, 0);
			mOutputStream = mSerialPort.getOutputStream();
			mInputStream = mSerialPort.getInputStream();
			if (dateReceiveCallback != null) {
				mReadThread = new ReadThread("gd-serial");
				mReadThread.start();
			}
			_isOpen = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public OutputStream getOutputStream() {
		return mOutputStream;
	}

	public InputStream getInputStream() {
		return mInputStream;
	}

	public void close() {
		mOutputStream = null;
		mInputStream = null;
		if (mReadThread != null) {
			mReadThread.interrupt();
			mReadThread = null;
		}
		if (mSerialPort != null) {
			mSerialPort.close();
			mSerialPort = null;
		}
		_isOpen = false;
	}

	public void send(byte[] bOutArray) {
		try {
			if (null != mOutputStream) {
				mOutputStream.write(bOutArray);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void sendHex(String sHex) {
		byte[] bOutArray = MyFunc.HexToByteArr(sHex);
		send(bOutArray);
	}

	public void sendTxt(String sTxt) {
		byte[] bOutArray = sTxt.getBytes();
		send(bOutArray);
	}

	private class ReadThread extends Thread {
		public ReadThread(String name) {
			setName(name);
		}

		@Override
		public void run() {
			super.run();
			while (!isInterrupted()) {
				try {
					if (mInputStream == null) {
						return;
					}
					byte[] buffer = new byte[512];
					int size = mInputStream.read(buffer);
					if (size > 0) {
						android_serialport_api.bean.ComBean ComRecData = new android_serialport_api.bean.ComBean(sPort,
								buffer, size);
						dateReceiveCallback.onDataReceived(ComRecData);
//                        LogUtils.printI( "<===>comTecData " + ComRecData.toString());
					}
					try {
						// 延时50ms
						Thread.sleep(50);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				} catch (Throwable e) {
					e.printStackTrace();
					return;
				}
			}
		}
	}

	public int getBaudRate() {
		return iBaudRate;
	}

	public boolean setBaudRate(int iBaud) {
		if (_isOpen) {
			return false;
		} else {
			iBaudRate = iBaud;
			return true;
		}
	}

	public boolean setBaudRate(String sBaud) {
		int iBaud = Integer.parseInt(sBaud);
		return setBaudRate(iBaud);
	}

	public String getPort() {
		return sPort;
	}

	public boolean setPort(String sPort) {
		if (_isOpen) {
			return false;
		} else {
			this.sPort = sPort;
			return true;
		}
	}

	public boolean isOpen() {
		return _isOpen;
	}

}